//
//  ModifyPasswordVC.swift
//  Employee
//
//  Created by swordjoy on 2017/5/2.
//  Copyright © 2017年 remotevillage. All rights reserved.
//

import UIKit

class ModifyPasswordVC: BaseVC {

  var phone: String!
  
  fileprivate var contentV: UIView!
  
  fileprivate var passwordTextField: UITextField!
  
  fileprivate var repeatTextField: UITextField!
  
  fileprivate var okButton: UIButton!
  
  override func viewDidLoad() {
    super.viewDidLoad()
    view.backgroundColor = UIColor.white
    title = "新密码"
    
    create()
  }
  
  override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)
    GlobalConfig.baiduStatisticsPageViewStart("修改密码")
  }
    override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)
        GlobalConfig.baiduStatisticsPageViewEnd("修改密码")
    }

}

// MARK: - action

extension ModifyPasswordVC {
  
  @objc fileprivate func okButtonHelper() {
    
    if !passwordTextField.text!.isEmpty && !repeatTextField.text!.isEmpty {
      okButton.backgroundColor = GlobalConfig.BGSkyBlueColor
      okButton.isEnabled = true
    }
    else {
      okButton.backgroundColor = GlobalConfig.TextLightColor
      okButton.isEnabled = false
    }
    
  }
  
  @objc fileprivate func passwordChangeAction() {
    
    okButtonHelper()
    
  }
  
  @objc fileprivate func repeatChangeAction() {
  
    okButtonHelper()
    
  }
  
  @objc fileprivate func okAction() {
    
    SJApp.endEdit()
    
    if passwordTextField.text!.sj.length < 6 {
      SJToast.failShow("密码长度不能小于6位")
      return
    }
    
    if passwordTextField.text! != repeatTextField.text! {
      SJToast.failShow("两次密码输入不一致")
      return
    }
    
    if !SJApp.isShowHUD { SJApp.showHUD() }
    let _ = UserRequest.shared.resetPassword(phone: phone, password: passwordTextField.text!) {
        SJToast.successShow("成功重置密码")
      self.navigationController?.popToRootViewController(animated: true)
    }
    
  }
  
}

// MARK: - fileprivate

extension ModifyPasswordVC {
  
  fileprivate func create() {
    
    addBackItem()
    
    contentV = UIView(frame: UIScreen.main.bounds)
    contentV.backgroundColor = GlobalConfig.BGWhiteColor
    view.addSubview(contentV)
    
    passwordTextField = UITextField()
    contentV.addSubview(passwordTextField)
    passwordTextField.addTarget(self, action: #selector(passwordChangeAction), for: .editingChanged)
    passwordTextField.placeholder = "新密码"
    passwordTextField.leftView = UIView(frame: CGRect(x: 0, y: 0, width: 15, height: 50))
    passwordTextField.leftViewMode = .always
    passwordTextField.clearButtonMode = .whileEditing
    passwordTextField.backgroundColor = UIColor.white
    passwordTextField.isSecureTextEntry = true
    passwordTextField.snp.makeConstraints { (make) in
      make.left.equalTo(10)
      make.right.equalTo(-10)
      make.height.equalTo(50)
      make.top.equalTo(10)
    }
    passwordTextField.layer.cornerRadius = 4
    passwordTextField.clipsToBounds = true
    
    repeatTextField = UITextField()
    contentV.addSubview(repeatTextField)
    repeatTextField.addTarget(self, action: #selector(repeatChangeAction), for: .editingChanged)
    repeatTextField.placeholder = "重复新密码"
    repeatTextField.leftView = UIView(frame: CGRect(x: 0, y: 0, width: 15, height: 50))
    repeatTextField.leftViewMode = .always
    repeatTextField.clearButtonMode = .whileEditing
    repeatTextField.backgroundColor = UIColor.white
    repeatTextField.isSecureTextEntry = true
    repeatTextField.snp.makeConstraints { (make) in
      make.left.equalTo(10)
      make.right.equalTo(-10)
      make.height.equalTo(50)
      make.top.equalTo(passwordTextField.snp.bottom).offset(10)
    }
    repeatTextField.layer.cornerRadius = 4
    repeatTextField.clipsToBounds = true
    
    okButton = UIButton()
    contentV.addSubview(okButton)
    okButton.setTitle("确定", for: .normal)
    okButton.setTitleColor(UIColor.white, for: .normal)
    okButton.titleLabel?.font = GlobalConfig.system15Font
    okButton.addTarget(self, action: #selector(okAction), for: .touchUpInside)
    okButton.snp.makeConstraints { (make) in
      make.left.equalTo(10)
      make.right.equalTo(-10)
      make.height.equalTo(50)
      make.top.equalTo(repeatTextField.snp.bottom).offset(20)
    }
    okButton.setBackgroundImage(UIImage.getLinerGradientImage(with: UIColor.hex("#2D96F1"), endColor: UIColor.hex("#04BEFE"), size: CGSize(width: UIScreen.width, height: 50)), for: .normal)
    okButton.layer.cornerRadius = 4
    okButton.clipsToBounds = true
    
    okButtonHelper()
    
  }
  
}
